/**
 * 
 */
package com.xbang.attendance.entity.system;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import com.xbang.attendance.entity.product.Product;

import enumeration.WarningEqualesType;
import enumeration.WarningType;

/**
 * @author dev.zhang
 * 
 *         预警规则配置
 */
@Entity
public class EntityWarning implements Serializable {

	private static final long serialVersionUID = 303974351749301365L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;

	/** 预警规则编码 */
	private String code;

	/** 预警规则名称 */
	private String name;

	/** 对应仓库-可以不对应 */
	@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST }, targetEntity = Warehouse.class)
	@JoinColumn(columnDefinition = "warehouse", referencedColumnName = "id")
	private Warehouse warehouse;

	/** 对应产品，可以不对应 */
	@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST }, targetEntity = Product.class)
	@JoinColumn(columnDefinition = "product", referencedColumnName = "id")
	private Product product;

	/** 预警类型--百分比还是常量对比 */
	@Enumerated(EnumType.STRING)
	private WarningType warningType;

	/** 额定百分比 */
	private BigDecimal percentage;

	// private

	/** 数量 */
	private BigDecimal number;

	/** 对应计算 */
	@Enumerated(EnumType.STRING)
	private WarningEqualesType warningEqualesType;

	/** 描述 */
	private String description;

	/**
	 * 对比是否超过常量安全阀值
	 * 
	 * @param constantsAmount
	 * @param warningEqualesType
	 * @param indentAmount
	 * @return
	 */
	public boolean entityWarningEqules(BigDecimal constantsAmount, WarningEqualesType warningEqualesType, BigDecimal indentAmount) {

		if (this.warningEqualesType.equals(WarningEqualesType.EQ)) {
			return constantsAmount.compareTo(indentAmount) == 0;
		} else if (this.warningEqualesType.equals(WarningEqualesType.LEQ)) {
			return indentAmount.floatValue() <= constantsAmount.floatValue();
		} else if (this.warningEqualesType.equals(WarningEqualesType.LQ)) {
			return indentAmount.floatValue() < constantsAmount.floatValue();
		} else if (this.warningEqualesType.equals(WarningEqualesType.GQ)) {
			return indentAmount.floatValue() > constantsAmount.floatValue();
		} else if (this.warningEqualesType.equals(WarningEqualesType.GEQ)) {
			return indentAmount.floatValue() >= constantsAmount.floatValue();
		}

		return false;
	}

	/**
	 * 对比是否超过百分比安全阀值
	 * 
	 * @param constantsAmount
	 * @param percentAmount
	 * @param warningEqualesType
	 * @param indentAmount
	 * @return
	 */
	public boolean entityWarningEqules(BigDecimal constantsAmount, BigDecimal percentAmount, WarningEqualesType warningEqualesType, BigDecimal indentAmount) {

		if (constantsAmount == null) {
			constantsAmount = new BigDecimal(0);
		}

		if (percentAmount == null) {
			percentAmount = new BigDecimal(0);
		}

		BigDecimal sumAmount = constantsAmount.multiply(percentAmount);
		return entityWarningEqules(sumAmount, warningEqualesType, indentAmount);
	}

	
	public String getWaringTypeDisplay() {
		if (this.warningType.equals(WarningType.constants)) {
			return "常量对比";
		} else
			return "百分比对比";
	}

	public String getOperationDislplay() {

		if (this.warningEqualesType.equals(WarningEqualesType.EQ)) {
			return "等于";
		} else if (this.warningEqualesType.equals(WarningEqualesType.LEQ)) {
			return "小于等于";
		} else if (this.warningEqualesType.equals(WarningEqualesType.LQ)) {
			return "小于";
		} else if (this.warningEqualesType.equals(WarningEqualesType.GQ)) {
			return "大于";
		} else if (this.warningEqualesType.equals(WarningEqualesType.GEQ)) {
			return "大于等于";
		}

		return "";
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code
	 *            the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the warehouse
	 */
	public Warehouse getWarehouse() {
		return warehouse;
	}

	/**
	 * @param warehouse
	 *            the warehouse to set
	 */
	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}

	/**
	 * @return the product
	 */
	public Product getProduct() {
		return product;
	}

	/**
	 * @param product
	 *            the product to set
	 */
	public void setProduct(Product product) {
		this.product = product;
	}

	/**
	 * @return the number
	 */
	public BigDecimal getNumber() {
		return number;
	}

	/**
	 * @param number
	 *            the number to set
	 */
	public void setNumber(BigDecimal number) {
		this.number = number;
	}

	/**
	 * @return the warningEqualesType
	 */
	public WarningEqualesType getWarningEqualesType() {
		return warningEqualesType;
	}

	/**
	 * @param warningEqualesType
	 *            the warningEqualesType to set
	 */
	public void setWarningEqualesType(WarningEqualesType warningEqualesType) {
		this.warningEqualesType = warningEqualesType;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the percentage
	 */
	public BigDecimal getPercentage() {
		return percentage;
	}

	/**
	 * @param percentage
	 *            the percentage to set
	 */
	public void setPercentage(BigDecimal percentage) {
		this.percentage = percentage;
	}

	/**
	 * @return the warningType
	 */
	public WarningType getWarningType() {
		return warningType;
	}

	/**
	 * @param warningType
	 *            the warningType to set
	 */
	public void setWarningType(WarningType warningType) {
		this.warningType = warningType;
	}

}
